package com.keen.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class ConfigUtil {
	private static String configName = "conf.properties";
	private static Map<String,Properties> properties = new HashMap<String, Properties>();
	public static String getConfigName() {
		return configName;
	}


//	public static void setConfigName(String configName) {
//		ConfigUtil.configName = configName;
//	}


	public static Properties getProperties(String cfgName) throws IOException{
		if(properties.get(cfgName) == null){
			properties.put(cfgName, new Properties());
			try {
				properties.get(cfgName).load(ConfigUtil.class.getClassLoader().getResourceAsStream(cfgName));
			} catch (IOException e) {
				//logger.error("读取配置文件"+cfgName+"异常!",e);
				throw e;
			}
		}
		return properties.get(cfgName);
	}
	
	public static String getValue(String cfgName,String key) throws IOException{
		return getProperties(cfgName).getProperty(key);
	}
	
	public static String getValue2(String cfgName,String key){
		try {
			return getProperties(cfgName).getProperty(key);
		} catch (IOException e) {
		}
		return null;
	}
	
	public static String getValue(String key){
		try{
			if(configName!=null&&!configName.trim().equals("")){
				return getProperties(configName).getProperty(key);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 默认为null
	 * @param cfgName
	 * @param key
	 * @return
	 */
	public static String getString(String cfgName,String key){
		String rs = null;
		try {
			rs = getValue(cfgName, key);
		} catch (IOException e) {
		}
		return rs;
	}
	
	/**
	 * 默认为0
	 * @param cfgName
	 * @param key
	 * @return
	 */
	public static int getInt(String cfgName,String key){
		int rs = 0;
		try {
			rs = Integer.parseInt(getValue(cfgName, key));
		} catch (IOException e) {
		}
		return rs;
	}
	
	/**
	 * 默认为0
	 * @param cfgName
	 * @param key
	 * @return
	 */
	public static long getLong(String cfgName,String key){
		long rs = 0;
		try {
			rs = Long.parseLong(getValue(cfgName, key));
		} catch (IOException e) {
		}
		return rs;
	}
	
	/**
	 * 默认为false
	 * @param cfgName
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(String cfgName,String key){
		boolean rs = false;
		try {
			rs = Boolean.getBoolean(getValue(cfgName, key));
		} catch (IOException e) {
		}
		return rs;
	}
	
	public static void main(String[] args) {
			try {
				System.out.println(getValue("test3.properties","sub_ips"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	}
}
